﻿
namespace Insight.Web.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.ComponentModel.DataAnnotations;
    using System.Data;
    using System.Linq;
    using System.ServiceModel.DomainServices.EntityFramework;
    using System.ServiceModel.DomainServices.Hosting;
    using System.ServiceModel.DomainServices.Server;
    using Insight.Web.Data;


    // Implements application logic using the SecurityEntitiesConnection context.
    // TODO: Add your application logic to these methods or in additional methods.
    // TODO: Wire up authentication (Windows/ASP.NET Forms) and uncomment the following to disable anonymous access
    // Also consider adding roles to restrict access as appropriate.
    // [RequiresAuthentication]
    [EnableClientAccess()]
    public class SecurityDomainService : LinqToEntitiesDomainService<SecurityEntitiesConnection>
    {

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Applications' query.
        public IQueryable<Application> GetApplications()
        {
            return this.ObjectContext.Applications;
        }

        public void InsertApplication(Application application)
        {
            if ((application.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(application, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Applications.AddObject(application);
            }
        }

        public void UpdateApplication(Application currentApplication)
        {
            this.ObjectContext.Applications.AttachAsModified(currentApplication, this.ChangeSet.GetOriginal(currentApplication));
        }

        public void DeleteApplication(Application application)
        {
            if ((application.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Applications.Attach(application);
            }
            this.ObjectContext.Applications.DeleteObject(application);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Profiles' query.
        public IQueryable<Profile> GetProfiles()
        {
            return this.ObjectContext.Profiles;
        }

        public void InsertProfile(Profile profile)
        {
            if ((profile.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(profile, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Profiles.AddObject(profile);
            }
        }

        public void UpdateProfile(Profile currentProfile)
        {
            this.ObjectContext.Profiles.AttachAsModified(currentProfile, this.ChangeSet.GetOriginal(currentProfile));
        }

        public void DeleteProfile(Profile profile)
        {
            if ((profile.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Profiles.Attach(profile);
            }
            this.ObjectContext.Profiles.DeleteObject(profile);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Roles' query.
        public IQueryable<Role> GetRoles()
        {
            return this.ObjectContext.Roles;
        }

        public void InsertRole(Role role)
        {
            if ((role.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(role, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Roles.AddObject(role);
            }
        }

        public void UpdateRole(Role currentRole)
        {
            this.ObjectContext.Roles.AttachAsModified(currentRole, this.ChangeSet.GetOriginal(currentRole));
        }

        public void DeleteRole(Role role)
        {
            if ((role.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Roles.Attach(role);
            }
            this.ObjectContext.Roles.DeleteObject(role);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'Users' query.
        public IQueryable<User> GetUsers()
        {
            return this.ObjectContext.Users;
        }

        public void InsertUser(User user)
        {
            if ((user.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(user, EntityState.Added);
            }
            else
            {
                this.ObjectContext.Users.AddObject(user);
            }
        }

        public void UpdateUser(User currentUser)
        {
            this.ObjectContext.Users.AttachAsModified(currentUser, this.ChangeSet.GetOriginal(currentUser));
        }

        public void DeleteUser(User user)
        {
            if ((user.EntityState == EntityState.Detached))
            {
                this.ObjectContext.Users.Attach(user);
            }
            this.ObjectContext.Users.DeleteObject(user);
        }

        // TODO:
        // Consider constraining the results of your query method.  If you need additional input you can
        // add parameters to this method or create additional query methods with different names.
        // To support paging you will need to add ordering to the 'UsersInRoles' query.
        public IQueryable<UserInRole> GetUsersInRoles()
        {
            return this.ObjectContext.UsersInRoles;
        }

        public void InsertUserInRole(UserInRole userInRole)
        {
            if ((userInRole.EntityState != EntityState.Detached))
            {
                this.ObjectContext.ObjectStateManager.ChangeObjectState(userInRole, EntityState.Added);
            }
            else
            {
                this.ObjectContext.UsersInRoles.AddObject(userInRole);
            }
        }

        public void UpdateUserInRole(UserInRole currentUserInRole)
        {
            this.ObjectContext.UsersInRoles.AttachAsModified(currentUserInRole, this.ChangeSet.GetOriginal(currentUserInRole));
        }

        public void DeleteUserInRole(UserInRole userInRole)
        {
            if ((userInRole.EntityState == EntityState.Detached))
            {
                this.ObjectContext.UsersInRoles.Attach(userInRole);
            }
            this.ObjectContext.UsersInRoles.DeleteObject(userInRole);
        }
    }
}


